Page({
  data: {
    orderNumber: '',
    productName: '',
    applicant: '',
    expressNumber: ''
  },

  onLoad(options) {
    const { id } = options
    const pages = getCurrentPages()
    const prevPage = pages[pages.length - 2]
    const express = prevPage.data.expressList.find(item => item.orderNumber === id)
    
    if (express) {
      this.setData({
        orderNumber: express.orderNumber,
        productName: express.productName,
        applicant: express.applicant,
        expressNumber: express.expressNumber
      })
    }
  },

  handleOrderInput(e) {
    this.setData({
      orderNumber: e.detail.value
    })
  },

  handleExpressInput(e) {
    this.setData({
      expressNumber: e.detail.value
    })
  },

  saveExpress() {
    const { orderNumber, expressNumber } = this.data
    
    if (!orderNumber || !expressNumber) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      })
      return
    }

    const pages = getCurrentPages()
    const prevPage = pages[pages.length - 2]
    
    // 更新快递单号列表
    const newExpressList = prevPage.data.expressList.map(item => {
      if (item.orderNumber === this.data.orderNumber) {
        return {
          ...item,
          orderNumber: orderNumber,
          expressNumber: expressNumber
        }
      }
      return item
    })
    
    prevPage.setData({
      expressList: newExpressList
    })

    wx.navigateBack()
  }
})